package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsBagInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UseOptionalBox extends BaseUseGoods {

    private Property property;

    public UseOptionalBox(GamePlayer player, GoodsInfo goodsInfo, long param, String param2) {
        super(player, goodsInfo, param, param2);
        property = PropertyHelper.parseStringToProperty(param2);
    }

    @Override
    int checkUse(long count) {
        // 获取宝箱可选内容
        List<GoodsBagInfo> infoList = GoodsBagMgr.getGoodsBagInfoList(goodsInfo.getGoodsId());
        // 取不到就去中心库取
        if (infoList.isEmpty()) {
            Map<Integer, List<GoodsBagInfo>> map = CommonMgr.getGoodsBagInfoMap().get(goodsInfo.getGoodsId());
            if (map != null) {
                infoList = new ArrayList<>();
                for (List<GoodsBagInfo> value : map.values()) {
                    infoList.addAll(value);
                }
            } else {
                return GameErrorCode.E_BAG_OPTIONAL_BOX_CHOOSE_ERROR;
            }
        }

        if(goodsInfo.getType() == eGoodsType.OptionalBoxEx.getValue()){//拓展的自选礼包
            int result = checkNeedSelectPatronsOrBeauty(infoList);
            if(0 != result){
                return result;
            }
        }

        int curCount = 0;
        // 判断所选奖励所需宝箱数量是否正确
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            int goodsId = entry.getKey();
            BigInteger goodsCount = entry.getValue();
            // 数量校验
            if (goodsCount.compareTo(BigInteger.ZERO) <= 0) {
                return GameErrorCode.E_BAG_OPTIONAL_BOX_CHOOSE_ERROR;
            }
            // 能否选择校验
            if (!isGoodsCanChoose(goodsId, goodsCount)) {
                return GameErrorCode.E_BAG_OPTIONAL_BOX_CHOOSE_ERROR;
            }
            boolean isFind = false;
            for (GoodsBagInfo info : infoList) {
                if (info.getGoodsId() == goodsId) {
                    if (goodsCount.intValue() % info.getMinNum() != 0) {
                        // 校验是否是整数倍
                        return GameErrorCode.E_BAG_OPTIONAL_BOX_CHOOSE_ERROR;
                    }
                    int tempCount = goodsCount.intValue() / info.getMinNum();
                    curCount += tempCount;
                    isFind = true;
                    break;
                }
            }
            // 宝箱内没有该道具可选
            if (!isFind) {
                return GameErrorCode.E_BAG_OPTIONAL_BOX_CHOOSE_ERROR;
            }
        }
        if (curCount != count) {
            return GameErrorCode.E_BAG_OPTIONAL_BOX_CHOOSE_ERROR;
        }
        return 0;
    }

    @Override
    UseGoodsResult executeUse(long count) {
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Bag, eLogMoneyType.BagUse);

        UseGoodsResult result = new UseGoodsResult();
        result.setReward(param2);
        return result;
    }

    /**
     * 道具是否可选择
     */
    private boolean isGoodsCanChoose(int goodsId, BigInteger count) {
        // 【门客道具】只能选一个，无法重复获得
        if (GoodsMgr.isPatronsGoods(goodsId)) {
            if (count.compareTo(BigInteger.ONE) != 0) {
                return false;
            }
            List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getUserPatronsList();
            if (patronsList.stream().anyMatch(item -> item.getPatronsId() == goodsId)) {
                return false;
            }
            // 【美女道具】只能选一个，无法重复获得
        } else if (GoodsMgr.isBeautyGoods(goodsId)) {
            if (count.compareTo(BigInteger.ONE) != 0) {
                return false;
            }
            Map<Integer, UserBeauty> beautyMap = player.getModule(BeautyModule.class).getBeautyMap();
            if (beautyMap.values().stream().anyMatch(item -> item.getBeautyId() == goodsId)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 拓展自选礼包，需要先拥有门客、美女，才能选择道具
     * @param infoList
     * @return
     */
    private int checkNeedSelectPatronsOrBeauty(List<GoodsBagInfo> infoList){
        //先看看自选的道具里面有没有不是门客、美女的道具
        boolean allPatronsBeauty = true;
        for (Map.Entry<Integer, BigInteger> entryData : property.getGoods().entrySet()) {
            if(!GoodsMgr.isPatronsGoods(entryData.getKey()) && !GoodsMgr.isBeautyGoods(entryData.getKey())){
                allPatronsBeauty = false;
            }
        }
        if(allPatronsBeauty){//自选都是选择门客、美女，不用再往下判断
            return 0;
        }

        List<Integer> configPatronsIdList = new ArrayList<>();
        List<Integer> configBeautyIdList = new ArrayList<>();
        for(GoodsBagInfo info : infoList){
            if(GoodsMgr.isPatronsGoods(info.getGoodsId())){//门客
                if(!configPatronsIdList.contains(info.getGoodsId())){
                    configPatronsIdList.add(info.getGoodsId());
                }
            }else if(GoodsMgr.isBeautyGoods(info.getGoodsId())){//美女
                if(!configBeautyIdList.contains(info.getGoodsId())){
                    configBeautyIdList.add(info.getGoodsId());
                }
            }
        }
        if(configPatronsIdList.size() > 0){
            //先判断拥有的门客
            List<UserPatrons> hasPatronsList = player.getModule(PatronsModule.class).getUserPatronsList();
            for(UserPatrons userPatrons : hasPatronsList){
                if(configPatronsIdList.contains(userPatrons.getPatronsId())){
                    configPatronsIdList.remove(Integer.valueOf(userPatrons.getPatronsId()));     //移除拥有的门客
                }
            }
            //再判断自选的门客
            for (Map.Entry<Integer, BigInteger> entryData : property.getGoods().entrySet()) {
                if(GoodsMgr.isPatronsGoods(entryData.getKey())){
                    if(configPatronsIdList.contains(entryData.getKey())){
                        configPatronsIdList.remove(entryData.getKey());     //移除自选的门客
                    }
                }
            }

            if(configPatronsIdList.size() > 0){
                return GameErrorCode.E_BAG_OPTIONAL_BOX_NEED_SELECT_PATRONS;
            }
        }

        if(configBeautyIdList.size() > 0){
            //先判断拥有的美女
            Map<Integer, UserBeauty> beautyMap = player.getModule(BeautyModule.class).getBeautyMap();
            for(UserBeauty userBeauty : beautyMap.values()){
                if(configBeautyIdList.contains(userBeauty.getBeautyId())){
                    configBeautyIdList.remove(Integer.valueOf(userBeauty.getBeautyId()));     //移除拥有的美女
                }
            }
            //再判断自选的美女
            for (Map.Entry<Integer, BigInteger> entryData : property.getGoods().entrySet()) {
                if(GoodsMgr.isBeautyGoods(entryData.getKey())){
                    if(configBeautyIdList.contains(entryData.getKey())){
                        configBeautyIdList.remove(entryData.getKey());     //移除自选的美女
                    }
                }
            }

            if(configBeautyIdList.size() > 0){
                return GameErrorCode.E_BAG_OPTIONAL_BOX_NEED_SELECT_BEAUTY;
            }
        }
        return 0;
    }
}
